import {messages} from "share/common";
import React,{Component} from 'react';
import {Button, Modal} from 'antd';
import AdvanceTable from 'components/advanced-table';
import DataOfColumns from './data-of-column';
import DataOfSearchForm from './data-of-search-form';
import GeneralViewServer from './general-view-server';
import {message} from "antd/lib/index";
import openSlideFrame from "share/openSlideFrame";
import FictitiousProgress from 'containers/supplier-management/order-center/components/fictitious-progress';
import 'styles/financial-management/general-view/general-view.scss'

class GeneralView extends Component{

  constructor(props){
    super(props);

    this.columns = DataOfColumns.getGeneralViewColumns();
    this.searchForm = DataOfSearchForm.getGeneralViewSearchForm();
    this.searchParams = {
      page: 0,
      size: 10,
      keyName: '',
      sortType: 'desc',// 'ASC'
    },
    this.state = {
      showDownloadModal: false,// 下载前生成文件进度条
      isCreated: false, // 是否已经生成
      downloadUrl: '',
      isFail: false,
    }
  }

  createTableOption = () => {
    return {
      key: 'plane',
      tableName: 'general-view-tables',
      title: messages('supplier-6.key14')/*通用单据查看*/,
      service: this.advanceTableServer,
      index: true,
      onRef: this.advanceTableRef,
      rowKey: record => record.id,
      columns: this.columns,
      onChange: this.onSortChange,
      searchForm: this.searchForm,
      footer: <Button type='primary' onClick={this.downloadExcel}>{messages('supplier-6.key15')/*导出搜索数据*/}</Button>,
      bordered: true,
      onRow: (record) => {
        return {
          onClick: () => {
            openSlideFrame.openOtherReport({
              formOID: record.formOID,
              entityOID: record.entityOID,
              status: 'approving',
              approverOID: record.approverOID,
              entry: 'slide',
              slideReadOnly: true
            }, {
              afterClose: value => {
                typeof value === 'boolean' && value && this.tableRef.getList();
              },
            }, this);
          }
        };
      }
    }
  }

  onSortChange = (pagination, filter, sorter) => {
    if (sorter.order) {
      this.searchParams.keyName = sorter.columnKey;
      this.searchParams.sortType = sorter.order === 'ascend' ? 'asc' : 'desc';
      this.tableRef.getList();
    }
  };

  advanceTableRef = (ref) => {
    this.tableRef = ref;
  }

  advanceTableServer = (page, size, searchValues) => {
    this.searchParams = Object.assign({},this.searchParams, {page: page, size: size}, searchValues);
    return GeneralViewServer(this).getGeneralViewData(this.searchParams);
  }

  // 导出表格
  downloadExcel = () => {
    this.setState({
      showDownloadModal: true,
      isFail: false
    });
    GeneralViewServer(this).exportGeneralViewData(this.searchParams).then(res => {
      if(res && res.data.path){
        this.setState({
          downloadUrl: res.data.path,
          isCreated: true,
        })
      }else{
        this.setState({
          isFail: true,
        })
        this.onCancelDownload();
        message.error(res.data.errorDetail);
      }
    })
  }

  /**
   * 取消下载
   */
  onCancelDownload = () => {
    this.setState({
      showDownloadModal: false,
      isCreated: false,
      downloadUrl: '',
    })
  }


  render(){
    return (
      <div className='general-view-box'>
        <div style={{height: 'calc(100vh - 64px)'}}>
          <AdvanceTable {...this.createTableOption()} />
        </div>
        <FictitiousProgress onCancel={this.onCancelDownload} {...this.state}/>
      </div>
    )
  }
}

export default GeneralView;
